
#include <renderer.h>
#include <standard/texturetarget.h>
#include <standard/notification.h>
#include <standard/gc.h>
#include "workcanvas.h"
#include "editorscenemgr.h"
#include "command/commandmgr.h"
#include  "global.h"

CWorkCanvas::CWorkCanvas( float fLeft, float fTop, float fWidth, float fHeight ):
CTranslatePanel( fLeft, fTop, fWidth, fHeight ){
    m_bCtrlDown = False;
    m_bShiftDown = False;
    m_fCanvasWidth = fWidth - 8.0f;
    m_fCanvasHeight = fHeight - 8.0f;
}
CWorkCanvas::~CWorkCanvas(){
    //NOTIFICATION_REMOVE_DELEGATE( NEWMAPNOTIFICATION, this, CWorkCanvas::OnNotification_NewMap );
    //NOTIFICATION_REMOVE_DELEGATE( SAVEMAPNOTIFICATION, this, CWorkCanvas::OnNotification_SaveMap );
    //NOTIFICATION_REMOVE_DELEGATE( CLOSEMAPNOTIFICATION, this, CWorkCanvas::OnNotification_CloseMap );
    //NOTIFICATION_REMOVE_DELEGATE( SETOPERATORTYPE, this, CWorkCanvas::OnNotification_SetOperatorType );
    //NOTIFICATION_REMOVE_DELEGATE( MAINMENU_VIEW_ITEM_0, this, CWorkCanvas::OnNotification_MainMenu_View_Item_0 );
    //NOTIFICATION_REMOVE_DELEGATE( MAINMENU_VIEW_ITEM_6, this, CWorkCanvas::OnNotification_MainMenu_View_Item_6 );
    //NOTIFICATION_REMOVE_DELEGATE( MAINMENU_VIEW_ITEM_10, this, CWorkCanvas::OnNotification_MainMenu_View_Item_10 );
    //NOTIFICATION_REMOVE_DELEGATE( MAINMENU_VIEW_ITEM_12, this, CWorkCanvas::OnNotification_MainMenu_View_Item_12 );

    //NOTIFICATION_REMOVE_DELEGATE( MAINMENU_Test, this, CWorkCanvas::OnNotification_MainMenu_Test );
    //ReleaseMap();
}
void CWorkCanvas::Init(){
    m_rTextureTarget = NEW CTextureTarget( 1024, 1024, True, True );

    CImage*  _pMainCanvas = NEW CImage( 4, 4, m_fCanvasWidth, m_fCanvasHeight );
    this->AddElement(_pMainCanvas);
    _pMainCanvas->SetTextureCoordsInfo( 0, 1024, 1024, 0, m_fCanvasHeight, m_fCanvasWidth, 0 );
    _pMainCanvas->ApplyTextureCoordsInfo();
    _pMainCanvas->AddUIEvent( UEM_MOUSEEVENT );
    _pMainCanvas->AddUIEvent( UEM_KEYEVENT );
    _pMainCanvas->OnMouseEventObject = this;
    _pMainCanvas->OnKeyEventObject = this;
    _pMainCanvas->SetTexture( m_rTextureTarget.GetEntityData() );


    //NOTIFICATION_REGISTER( NEWMAPNOTIFICATION, 0, this, CWorkCanvas::OnNotification_NewMap );
    //NOTIFICATION_REGISTER( SAVEMAPNOTIFICATION, 0, this, CWorkCanvas::OnNotification_SaveMap );
    //NOTIFICATION_REGISTER( OPENMAPNOTIFICATION, 0, this, CWorkCanvas::OnNotification_OpenMap );
    //NOTIFICATION_REGISTER( CLOSEMAPNOTIFICATION, 0, this, CWorkCanvas::OnNotification_CloseMap );
    //
    NOTIFICATION_REGISTER( SETOPERATORTYPE, 0, this, CWorkCanvas::OnNotification_SetOperatorType );
    NOTIFICATION_REGISTER( SETCOMMANDTYPE, 0, this, CWorkCanvas::OnNotification_SetCommandType );
    NOTIFICATION_REGISTER( SETSCENEVISIBLE, 0, this, CWorkCanvas::OnNotification_SetSceneVisible );

    //NOTIFICATION_REGISTER(MAINMENU_VIEW_ITEM_0, 0, this, CWorkCanvas::OnNotification_MainMenu_View_Item_0);
    //NOTIFICATION_REGISTER(MAINMENU_VIEW_ITEM_6, 0, this, CWorkCanvas::OnNotification_MainMenu_View_Item_6);
    //NOTIFICATION_REGISTER(MAINMENU_VIEW_ITEM_10, 0, this, CWorkCanvas::OnNotification_MainMenu_View_Item_10);
    //NOTIFICATION_REGISTER(MAINMENU_VIEW_ITEM_12, 0, this, CWorkCanvas::OnNotification_MainMenu_View_Item_12);
    //
    //NOTIFICATION_REGISTER(MAINMENU_Test, 0, this, CWorkCanvas::OnNotification_MainMenu_Test);

    
}
bool32 CWorkCanvas::NewMap( const char* strResourceSpace, int32 nBlockCountX, int32 nBlockCountY, float fBlockSize, float fObjectUnitSize, const char* strSkyBox ){
    m_rSceneMgr = NEW CEditorSceneMgr();
    m_rSceneMgr->Init( m_rTextureTarget.GetEntityData(), m_fCanvasWidth, m_fCanvasHeight,  strResourceSpace, nBlockCountX, nBlockCountY, fBlockSize, fObjectUnitSize, strSkyBox );

    return True;
}
bool32 CWorkCanvas::SaveMap( const char* strFileName ){
    return m_rSceneMgr->Save( strFileName );
}
bool32 CWorkCanvas::OpenMap( CXML *pXML ){
    m_rSceneMgr = NEW CEditorSceneMgr();
    return m_rSceneMgr->Open( m_rTextureTarget.GetEntityData(), m_fCanvasWidth, m_fCanvasHeight, pXML );
}
int32 CWorkCanvas::Update( int32 nDelta, float fDelta ){
    if( m_rSceneMgr.IsAttached() ){
        m_rSceneMgr->Update( nDelta, fDelta );
    }

    return 0;
}
int32 CWorkCanvas::Render(){
    if( m_rSceneMgr.IsAttached() ){
        m_rSceneMgr->Render();
    }else{
        m_rTextureTarget->BeginRender( m_fCanvasWidth, m_fCanvasHeight );
        Renderer_SetClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
        Renderer_Clear( True, True );
        m_rTextureTarget->EndRender();
    }
    return 0;
}
void CWorkCanvas::OnNotification_SetOperatorType( int32 w, int32 l, void* pData ){
    m_rSceneMgr->SetOperator( (EOperatorType)w );
}
void CWorkCanvas::OnNotification_SetCommandType( int32 w, int32 l, void* pData ){
    if( w == 0 )
        m_rSceneMgr->GetCommandMgr()->Undo();
    else
        m_rSceneMgr->GetCommandMgr()->Redo();
}
void CWorkCanvas::OnNotification_SetSceneVisible( int32 w, int32 l, void* pData ){
    m_rSceneMgr->SetVisibled( w, l );
}
void CWorkCanvas::OnClick( CUIElement *pSelf ){

}
void CWorkCanvas::OnMouseEvent( CUIElement *pSelf, EUIMouseMessage eMouseMessage, int32 x, int32 y, int32 z, int32 s ){
    if( m_rSceneMgr.IsAttached() ){
        m_rSceneMgr->OnMouseEvent( eMouseMessage, x, y, z, s );
    }
}
void CWorkCanvas::OnKeyEvent( CUIElement *pSelf, EUIKeyMessage eKeyMessage, int32 nKey ){
    if( m_rSceneMgr.IsAttached() ){
        if( nKey == VK_CONTROL ){
            if( eKeyMessage == UKE_KEYDOWN ){
                g_bIsCtrlDown = True;
                NOTIFICATION_SEND( SETSTATUSTEXT, STATUS_CTRLKEY, 0, (void*)"Ctrl On" );
            } else{
                g_bIsCtrlDown = False;
                NOTIFICATION_SEND( SETSTATUSTEXT, STATUS_CTRLKEY, 0, (void*)"Ctrl Off" );
            }
            return;
        } else if( nKey == VK_SHIFT ){
            if( eKeyMessage == UKE_KEYDOWN ){
                g_bIsShiftDown = True;
                NOTIFICATION_SEND( SETSTATUSTEXT, STATUS_SHIFTKEY, 0, (void*)"Shift On" );
            }else{
                g_bIsShiftDown = False;
                NOTIFICATION_SEND( SETSTATUSTEXT, STATUS_SHIFTKEY, 0, (void*)"Shift Off" );

            }
            return;
        }
        m_rSceneMgr->OnKeyEvent( eKeyMessage, nKey );
    }
}

void CWorkCanvas::OnHotKey( int32 nID ){

}
void CWorkCanvas::Final(){
    //m_rSceneMgr->Final();
}
void CWorkCanvas::ReleaseMap(){
    if( m_rSceneMgr.IsAttached()){
        m_rSceneMgr->Final();
        m_rSceneMgr.Detach();
    }
    CGarbageCollection::GetInterface()->Sweep();
}
